// (c) Copyright Ascensio System SIA 2009-2024
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The  interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode

import HistoryReactSvgUrl from "PUBLIC_DIR/images/history.react.svg?url";
import HistoryFinalizedReactSvgUrl from "PUBLIC_DIR/images/history-finalized.react.svg?url";
import MoveReactSvgUrl from "PUBLIC_DIR/images/move.react.svg?url";
import CheckBoxReactSvgUrl from "PUBLIC_DIR/images/check-box.react.svg?url";
import FolderReactSvgUrl from "PUBLIC_DIR/images/folder.react.svg?url";
import ReconnectSvgUrl from "PUBLIC_DIR/images/reconnect.svg?url";
import SettingsReactSvgUrl from "PUBLIC_DIR/images/catalog.settings.react.svg?url";
import FileActionsOwnerReactSvgUrl from "PUBLIC_DIR/images/file.actions.owner.react.svg?url";
import FolderLocationReactSvgUrl from "PUBLIC_DIR/images/folder.location.react.svg?url";
import TickRoundedSvgUrl from "PUBLIC_DIR/images/tick.rounded.svg?url";
import FavoritesReactSvgUrl from "PUBLIC_DIR/images/favorites.react.svg?url";
import DownloadReactSvgUrl from "PUBLIC_DIR/images/download.react.svg?url";
import DownloadAsReactSvgUrl from "PUBLIC_DIR/images/download-as.react.svg?url";
import RenameReactSvgUrl from "PUBLIC_DIR/images/rename.react.svg?url";
import RemoveSvgUrl from "PUBLIC_DIR/images/remove.svg?url";
import TrashReactSvgUrl from "PUBLIC_DIR/images/trash.react.svg?url";
import LockedReactSvgUrl from "PUBLIC_DIR/images/locked.react.svg?url";
import CopyReactSvgUrl from "PUBLIC_DIR/images/copy.react.svg?url";
import DuplicateReactSvgUrl from "PUBLIC_DIR/images/duplicate.react.svg?url";
import FormFillRectSvgUrl from "PUBLIC_DIR/images/form.fill.rect.svg?url";
import AccessEditReactSvgUrl from "PUBLIC_DIR/images/access.edit.react.svg?url";
import EyeReactSvgUrl from "PUBLIC_DIR/images/eye.react.svg?url";
import FormPlusReactSvgUrl from "PUBLIC_DIR/images/form.plus.react.svg?url";
import FormFileReactSvgUrl from "PUBLIC_DIR/images/form.file.react.svg?url";
import PersonReactSvgUrl from "PUBLIC_DIR/images/person.react.svg?url";
import InfoOutlineReactSvgUrl from "PUBLIC_DIR/images/info.outline.react.svg?url";
import PinReactSvgUrl from "PUBLIC_DIR/images/pin.react.svg?url";
import UnpinReactSvgUrl from "PUBLIC_DIR/images/unpin.react.svg?url";
import UnmuteReactSvgUrl from "PUBLIC_DIR/images/unmute.react.svg?url";
import MuteReactSvgUrl from "PUBLIC_DIR/images/icons/16/mute.react.svg?url";
import ShareReactSvgUrl from "PUBLIC_DIR/images/share.react.svg?url";
import InvitationLinkReactSvgUrl from "PUBLIC_DIR/images/invitation.link.react.svg?url";
import TabletLinkReactSvgUrl from "PUBLIC_DIR/images/tablet-link.react.svg?url";
import MailReactSvgUrl from "PUBLIC_DIR/images/mail.react.svg?url";
import RoomArchiveSvgUrl from "PUBLIC_DIR/images/room.archive.svg?url";
import PluginActionsSvgUrl from "PUBLIC_DIR/images/plugin.actions.react.svg?url";
import LeaveRoomSvgUrl from "PUBLIC_DIR/images/logout.react.svg?url";
import CatalogRoomsReactSvgUrl from "PUBLIC_DIR/images/catalog.rooms.react.svg?url";
import RemoveOutlineSvgUrl from "PUBLIC_DIR/images/remove.react.svg?url";
import PersonAdminReactSvgUrl from "PUBLIC_DIR/images/person.admin.react.svg?url";
import PersonManagerReactSvgUrl from "PUBLIC_DIR/images/person.manager.react.svg?url";
import PersonDefaultReactSvgUrl from "PUBLIC_DIR/images/person.default.react.svg?url";
import InviteAgainReactSvgUrl from "PUBLIC_DIR/images/invite.again.react.svg?url";
import PersonUserReactSvgUrl from "PUBLIC_DIR/images/person.user.react.svg?url";
import GroupReactSvgUrl from "PUBLIC_DIR/images/group.react.svg?url";
import ActionsDocumentsReactSvgUrl from "PUBLIC_DIR/images/actions.documents.react.svg?url";
import SpreadsheetReactSvgUrl from "PUBLIC_DIR/images/spreadsheet.react.svg?url";
import ActionsPresentationReactSvgUrl from "PUBLIC_DIR/images/actions.presentation.react.svg?url";
import FormReactSvgUrl from "PUBLIC_DIR/images/access.form.react.svg?url";
import FormBlankReactSvgUrl from "PUBLIC_DIR/images/form.blank.react.svg?url";
import FormGalleryReactSvgUrl from "PUBLIC_DIR/images/form.gallery.react.svg?url";
import CatalogFolderReactSvgUrl from "PUBLIC_DIR/images/catalog.folder.react.svg?url";
import ActionsUploadReactSvgUrl from "PUBLIC_DIR/images/actions.upload.react.svg?url";
import PluginMoreReactSvgUrl from "PUBLIC_DIR/images/plugin.more.react.svg?url";
import CodeReactSvgUrl from "PUBLIC_DIR/images/code.react.svg?url";
import ClearTrashReactSvgUrl from "PUBLIC_DIR/images/clear.trash.react.svg?url";

import { getCategoryUrl } from "@docspace/client/src/helpers/utils";

import { makeAutoObservable } from "mobx";
import copy from "copy-to-clipboard";
import saveAs from "file-saver";
import { isMobile, isTablet } from "react-device-detect";
import config from "PACKAGE_FILE";
import { toastr } from "@docspace/shared/components/toast";
import { combineUrl } from "@docspace/shared/utils/combineUrl";
import { isDesktop, trimSeparator } from "@docspace/shared/utils";
import { getDefaultAccessUser } from "@docspace/shared/utils/getDefaultAccessUser";
import { copyShareLink } from "@docspace/shared/utils/copy";

import { connectedCloudsTypeTitleTranslation } from "@docspace/client/src/helpers/filesUtils";
import { getOAuthToken } from "@docspace/shared/utils/common";
import api from "@docspace/shared/api";
import {
  RoomsType,
  Events,
  FolderType,
  UrlActionType,
  EmployeeType,
  FilesSelectorFilterTypes,
  FilterType,
  FileExtensions,
} from "@docspace/shared/enums";
import FilesFilter from "@docspace/shared/api/files/filter";
import { getFileLink, getFolderLink } from "@docspace/shared/api/files";
import { resendInvitesAgain } from "@docspace/shared/api/people";
import { checkDialogsOpen } from "@docspace/shared/utils/checkDialogsOpen";

const LOADER_TIMER = 500;
let loadingTime;
let timer;

const systemFolders = [
  FolderType.InProgress,
  FolderType.Done,
  FolderType.SubFolderDone,
  FolderType.SubFolderInProgress,
];

class ContextOptionsStore {
  settingsStore;
  dialogsStore;
  filesActionsStore;
  filesStore;
  mediaViewerDataStore;
  treeFoldersStore;
  uploadDataStore;
  versionHistoryStore;
  filesSettingsStore;
  selectedFolderStore;
  publicRoomStore;
  oformsStore;
  pluginStore;
  infoPanelStore;
  currentTariffStatusStore;
  userStore;
  clientLoadingStore;

  linksIsLoading = false;

  constructor(
    settingsStore,
    dialogsStore,
    filesActionsStore,
    filesStore,
    mediaViewerDataStore,
    treeFoldersStore,
    uploadDataStore,
    versionHistoryStore,
    filesSettingsStore,
    selectedFolderStore,
    publicRoomStore,
    oformsStore,
    pluginStore,
    infoPanelStore,
    currentTariffStatusStore,
    userStore,
    clientLoadingStore,
  ) {
    makeAutoObservable(this);
    this.settingsStore = settingsStore;
    this.dialogsStore = dialogsStore;
    this.filesActionsStore = filesActionsStore;
    this.filesStore = filesStore;
    this.mediaViewerDataStore = mediaViewerDataStore;
    this.treeFoldersStore = treeFoldersStore;
    this.uploadDataStore = uploadDataStore;
    this.versionHistoryStore = versionHistoryStore;
    this.filesSettingsStore = filesSettingsStore;
    this.selectedFolderStore = selectedFolderStore;
    this.publicRoomStore = publicRoomStore;
    this.oformsStore = oformsStore;
    this.pluginStore = pluginStore;
    this.infoPanelStore = infoPanelStore;
    this.currentTariffStatusStore = currentTariffStatusStore;
    this.userStore = userStore;
    this.clientLoadingStore = clientLoadingStore;
  }

  onOpenFolder = (item) => {
    this.filesActionsStore.openLocationAction(item);
  };

  onClickLinkFillForm = (item) => {
    if (!item.startFilling && item.isPDFForm)
      return this.dialogsStore.setFillPDFDialogData(true, item);

    return this.gotoDocEditor(false, item);
  };

  onClickReconnectStorage = async (item, t) => {
    const { thirdPartyStore } = this.filesSettingsStore;

    const { openConnectWindow, connectItems } = thirdPartyStore;

    const {
      setRoomCreation,
      setConnectItem,
      setConnectDialogVisible,
      setIsConnectDialogReconnect,
      setSaveAfterReconnectOAuth,
    } = this.dialogsStore;

    setIsConnectDialogReconnect(true);

    setRoomCreation(true);

    const provider = connectItems.find(
      (connectItem) => connectItem.providerName === item.providerKey,
    );

    const itemThirdParty = {
      title: connectedCloudsTypeTitleTranslation(provider.providerName, t),
      customer_title: "NOTITLE",
      provider_key: provider.providerName,
      link: provider.oauthHref,
      provider_id: item.providerId,
    };

    if (provider.isOauth) {
      let authModal = window.open(
        "",
        t("Common:Authorization"),
        "height=600, width=1020",
      );
      await openConnectWindow(provider.providerName, authModal)
        .then(getOAuthToken)
        .then((token) => {
          authModal.close();
          setConnectItem({
            ...itemThirdParty,
            token,
          });

          setSaveAfterReconnectOAuth(true);
        })
        .catch((err) => {
          if (!err) return;
          toastr.error(err);
        });
    } else {
      setConnectItem(itemThirdParty);
      setConnectDialogVisible(true);
    }
  };

  onClickMakeForm = (item, t) => {
    const { setConvertPasswordDialogVisible, setFormCreationInfo } =
      this.dialogsStore;
    const { title, id, folderId, fileExst } = item;

    const newTitle =
      title.substring(0, title.length - fileExst.length) +
      this.filesSettingsStore.extsWebRestrictedEditing[0];

    this.uploadDataStore.copyAsAction(id, newTitle, folderId).catch((err) => {
      let errorMessage = "";
      if (typeof err === "object") {
        errorMessage =
          err?.response?.data?.error?.message ||
          err?.statusText ||
          err?.message ||
          "";
      } else {
        errorMessage = err;
      }

      if (errorMessage.indexOf("password") == -1) {
        toastr.error(errorMessage, t("Common:Warning"));
        return;
      }

      toastr.error(t("Translations:FileProtected"), t("Common:Warning"));
      setFormCreationInfo({
        newTitle,
        fromExst: fileExst,
        toExst: this.filesSettingsStore.extsWebRestrictedEditing[0],
        fileInfo: item,
      });
      setConvertPasswordDialogVisible(true);
    });
  };

  onClickSubmitToFormGallery = (item) => {
    if (item && !item.exst) {
      const splitTitle = item.title.split(".");
      item.title = splitTitle.slice(0, -1).join(".");
      item.exst = splitTitle.length !== 1 ? `.${splitTitle.at(-1)}` : null;
    }

    this.dialogsStore.setFormItem(item);
    this.dialogsStore.setSubmitToGalleryDialogVisible(true);
  };

  onOpenLocation = (item) => {
    this.filesActionsStore.checkAndOpenLocationAction(item);
  };

  onOwnerChange = () => {
    this.dialogsStore.setChangeOwnerPanelVisible(true);
  };

  onMoveAction = (item) => {
    const { setIsMobileHidden } = this.infoPanelStore;
    const { id, isFolder } = this.selectedFolderStore;

    setIsMobileHidden(true);

    const isFolderActions = id === item?.id && isFolder === item?.isFolder;
    if (isFolderActions) {
      this.dialogsStore.setIsFolderActions(true);
    }

    this.dialogsStore.setMoveToPanelVisible(true);
  };

  onRestoreAction = () => {
    const { setIsMobileHidden } = this.infoPanelStore;
    setIsMobileHidden(true);
    this.dialogsStore.setRestorePanelVisible(true);
  };

  onCopyAction = (item) => {
    const { setIsMobileHidden } = this.infoPanelStore;
    const { id, isFolder } = this.selectedFolderStore;

    setIsMobileHidden(true);

    const isFolderActions = id === item?.id && isFolder === item?.isFolder;
    if (isFolderActions) {
      this.dialogsStore.setIsFolderActions(true);
    }

    this.dialogsStore.setCopyPanelVisible(true);
  };

  showVersionHistory = (id, security, requestToken) => {
    const { fetchFileVersions, setIsVerHistoryPanel } =
      this.versionHistoryStore;

    const { setIsMobileHidden } = this.infoPanelStore;

    if (this.treeFoldersStore.isRecycleBinFolder) return;

    fetchFileVersions(id + "", security, requestToken);
    setIsVerHistoryPanel(true);
    setIsMobileHidden(true);
  };

  finalizeVersion = (id) => {
    this.filesActionsStore.finalizeVersionAction(id).catch((err) => {
      toastr.error(err);
    });
  };

  onClickFavorite = (e, id, t) => {
    const data = (e.currentTarget && e.currentTarget.dataset) || e;
    const { action } = data;

    this.filesActionsStore
      .setFavoriteAction(action, id)
      .then(() =>
        action === "mark"
          ? toastr.success(t("MarkedAsFavorite"))
          : toastr.success(t("RemovedFromFavorites")),
      )
      .catch((err) => toastr.error(err));
  };

  lockFile = (item, t) => {
    const { id, locked } = item;
    const { setInfoPanelSelection: setInfoPanelSelection } =
      this.infoPanelStore;

    this.filesActionsStore
      .lockFileAction(id, !locked)
      .then(() =>
        locked
          ? toastr.success(t("Translations:FileUnlocked"))
          : toastr.success(t("Translations:FileLocked")),
      )
      .then(() => setInfoPanelSelection({ ...item, locked: !locked }))
      .catch((err) => {
        toastr.error(err);
      });
  };

  onClickLinkForPortal = (item, t) => {
    const { fileExst, canOpenPlayer, webUrl, id } = item;

    const isFile = !!fileExst;
    copy(
      isFile
        ? canOpenPlayer
          ? `${window.location.href}&preview=${id}`
          : webUrl
        : `${window.location.origin + config.homepage}/filter?folder=${id}`, //TODO: Change url by category
    );

    toastr.success(t("Common:LinkCopySuccess"));
  };

  onCopyLink = async (item, t) => {
    const { shared, navigationPath } = this.selectedFolderStore;

    const isArchive = item.rootFolderType === FolderType.Archive;

    const { href } = item;
    const sharedItem = navigationPath.find((r) => r.shared);

    const isShared = shared || sharedItem;

    const isSystemFolder = systemFolders.includes(item.type);

    if (isShared && !isArchive && !isSystemFolder) {
      try {
        const itemLink = item.isFolder
          ? await getFolderLink(item.id)
          : await getFileLink(item.id);
        copyShareLink(itemLink.sharedTo.shareLink);
        toastr.success(t("Common:LinkCopySuccess"));
      } catch (error) {
        toastr.error(error);
      }
      return;
    }

    if (
      item.rootFolderType === FolderType.Recent ||
      item.rootFolderType === FolderType.SHARE
    ) {
      copy(item.webUrl);
      return toastr.success(t("Common:LinkCopySuccess"));
    }

    if (href) {
      copy(href);

      return toastr.success(t("Common:LinkCopySuccess"));
    }

    const { canConvert } = this.filesSettingsStore;

    const { getItemUrl } = this.filesStore;

    const needConvert = canConvert(item.fileExst);

    const canOpenPlayer =
      item.viewAccessibility?.ImageView || item.viewAccessibility?.MediaView;

    const url = getItemUrl(
      item.id,
      item.isRoom || item.isFolder,
      needConvert,
      canOpenPlayer,
    );

    copy(url);

    toastr.success(t("Common:LinkCopySuccess"));
  };

  onOpenEmbeddingSettings = async (item) => {
    const { shared, navigationPath, getSelectedFolder } =
      this.selectedFolderStore;
    const { setLinkParams, setEmbeddingPanelData } = this.dialogsStore;

    const sharedItem = item.isRoom
      ? item
      : shared
        ? getSelectedFolder()
        : navigationPath.find((r) => r.shared);

    if (!sharedItem) return;

    const isPublicRoomType = sharedItem.roomType === RoomsType.PublicRoom;
    const isFormRoom = sharedItem.roomType === RoomsType.FormRoom;

    setLinkParams({
      roomId: sharedItem?.id,
      isPublic: isPublicRoomType,
      isFormRoom,
    });

    setEmbeddingPanelData({ visible: true, item });
  };

  onCreateAndCopySharedLink = async (item, t) => {
    const primaryLink = await this.filesStore.getPrimaryLink(item.id);

    if (primaryLink) {
      copyShareLink(primaryLink.sharedTo.shareLink);
      item.shared
        ? toastr.success(t("Common:LinkSuccessfullyCopied"))
        : toastr.success(t("Files:LinkSuccessfullyCreatedAndCopied"));

      this.publicRoomStore.setExternalLink(primaryLink);
    }
  };

  onClickLinkEdit = (item) => {
    const { setConvertItem, setConvertDialogVisible } = this.dialogsStore;
    const canConvert =
      item.viewAccessibility?.MustConvert && item.security?.Convert;

    if (canConvert) {
      setConvertItem({ ...item, isOpen: true });
      setConvertDialogVisible(true);
    } else {
      this.gotoDocEditor(false, item);
    }
  };

  onPreviewClick = (item) => {
    this.gotoDocEditor(true, item);
  };

  gotoDocEditor = (preview = false, item) => {
    const { id } = item;

    this.filesStore.openDocEditor(id, preview);
  };

  // isPwa = () => {
  //   return ["fullscreen", "standalone", "minimal-ui"].some(
  //     (displayMode) =>
  //       window.matchMedia("(display-mode: " + displayMode + ")").matches,
  //   );
  // };

  onClickDownload = (item, t) => {
    const { fileExst, contentLength, viewUrl } = item;
    const isFile = !!fileExst && contentLength;

    const { openUrl } = this.settingsStore;
    const { downloadAction } = this.filesActionsStore;

    isFile
      ? openUrl(viewUrl, UrlActionType.Download)
      : downloadAction(t("Translations:ArchivingData"), item).catch((err) =>
          toastr.error(err),
        );
  };

  onClickDownloadAs = () => {
    this.dialogsStore.setDownloadDialogVisible(true);
  };

  onClickCreateRoom = (item) => {
    this.filesActionsStore.setProcessCreatingRoomFromData(true);
    const event = new Event(Events.ROOM_CREATE);
    if (item && item.isFolder) {
      event.title = item.title;
    }
    window.dispatchEvent(event);
  };

  onDuplicate = (item) => {
    this.filesActionsStore.duplicateAction(item);
  };

  onClickRename = (item) => {
    const event = new Event(Events.RENAME);

    event.item = item;

    window.dispatchEvent(event);
  };

  onChangeThirdPartyInfo = (providerKey) => {
    this.filesActionsStore.setThirdpartyInfo(providerKey);
  };

  onMediaFileClick = (fileId, item) => {
    const itemId = typeof fileId !== "object" ? fileId : item.id;
    this.mediaViewerDataStore.setMediaViewerData({ visible: true, id: itemId });
    this.mediaViewerDataStore.changeUrl(itemId);
  };

  onClickDeleteSelectedFolder = (t, isRoom) => {
    const { setIsFolderActions, setDeleteDialogVisible, setIsRoomDelete } =
      this.dialogsStore;
    const { confirmDelete } = this.filesSettingsStore;
    const { deleteAction, deleteRoomsAction } = this.filesActionsStore;
    const { id: selectedFolderId, getSelectedFolder } =
      this.selectedFolderStore;
    const { isThirdPartySelection, getFolderInfo, setBufferSelection } =
      this.filesStore;

    setIsFolderActions(true);

    if (confirmDelete || isThirdPartySelection) {
      getFolderInfo(selectedFolderId).then((data) => {
        setBufferSelection(data);
        setIsRoomDelete(isRoom);
        setDeleteDialogVisible(true);
      });

      return;
    }

    let translations;

    if (isRoom) {
      translations = {
        successRemoveRoom: t("Files:RoomRemoved"),
        successRemoveRooms: t("Files:RoomsRemoved"),
      };

      deleteRoomsAction([selectedFolderId], translations).catch((err) =>
        toastr.error(err),
      );
    } else {
      translations = {
        deleteOperation: t("Translations:DeleteOperation"),
        deleteFromTrash: t("Translations:DeleteFromTrash"),
        deleteSelectedElem: t("Translations:DeleteSelectedElem"),
        FolderRemoved: t("Files:FolderRemoved"),
      };

      const selectedFolder = getSelectedFolder();

      deleteAction(translations, [selectedFolder], true).catch((err) =>
        toastr.error(err),
      );
    }
  };

  onClickDelete = (item, t) => {
    const { id, title, providerKey, rootFolderId, isFolder, isRoom } = item;

    const { setRemoveItem, setDeleteThirdPartyDialogVisible } =
      this.dialogsStore;

    if (id === this.selectedFolderStore.id && isFolder) {
      this.onClickDeleteSelectedFolder(t, isRoom);

      return;
    }

    const isRootThirdPartyFolder = providerKey && id === rootFolderId;

    if (isRootThirdPartyFolder) {
      const splitItem = id.split("-");
      setRemoveItem({ id: splitItem[splitItem.length - 1], title });
      setDeleteThirdPartyDialogVisible(true);
      return;
    }

    const translations = {
      deleteOperation: t("Translations:DeleteOperation"),
      successRemoveFile: t("Files:FileRemoved"),
      successRemoveFolder: t("Files:FolderRemoved"),
      successRemoveRoom: t("Files:RoomRemoved"),
      successRemoveRooms: t("Files:RoomsRemoved"),
    };

    this.filesActionsStore.deleteItemAction(
      id,
      translations,
      !isFolder,
      providerKey,
      isRoom,
    );
  };

  onClickShare = (item) => {
    const { openShareTab } = this.infoPanelStore;
    const { setShareFolderDialogVisible } = this.dialogsStore;

    if (item.isFolder) {
      setShareFolderDialogVisible(true);
    } else {
      openShareTab();
    }
  };

  onClickMarkRead = (item) => {
    const { markAsRead } = this.filesActionsStore;

    item.fileExst
      ? markAsRead([], [item.id], item)
      : markAsRead([item.id], [], item);
  };

  onClickUnsubscribe = () => {
    const { setDeleteDialogVisible, setUnsubscribe } = this.dialogsStore;

    setUnsubscribe(true);
    setDeleteDialogVisible(true);
  };

  onOpenPDFEditDialog = (id) => {
    this.filesStore.openDocEditor(id, false, null, true);

    // this.dialogsStore.setPdfFormEditVisible(true, id);
  };

  filterModel = (model, filter) => {
    let options = [];
    let index = 0;
    const last = model.length;

    for (index; index < last; index++) {
      if (filter.includes(model[index].key)) {
        options[index] = model[index];
        if (model[index].items) {
          options[index].items = model[index].items.filter((item) =>
            filter.includes(item.key),
          );

          if (options[index].items.length === 1) {
            options[index] = options[index].items[0];
          }
        }
      }
    }

    return options.filter((o) => !!o);
  };

  onShowInfoPanel = (item, view) => {
    const { setIsVisible, setView } = this.infoPanelStore;

    setIsVisible(true);
    view && setView(view);
  };

  onClickEditRoom = (item) => {
    const event = new Event(Events.ROOM_EDIT);
    event.item = item;
    window.dispatchEvent(event);
  };

  // onLoadLinks = async (t, item) => {
  //   const promise = new Promise(async (resolve, reject) => {
  //     let linksArray = [];

  //     this.setLoaderTimer(true);
  //     try {
  //       const links = await this.publicRoomStore.fetchExternalLinks(item.id);

  //       for (let link of links) {
  //         const { id, title, shareLink, disabled, isExpired } = link.sharedTo;

  //         if (!disabled && !isExpired) {
  //           linksArray.push({
  //             icon: InvitationLinkReactSvgUrl,
  //             id,
  //             key: `external-link_${id}`,
  //             label: title,
  //             onClick: () => {
  //               copy(shareLink);
  //               toastr.success(t("Common:LinkCopySuccess"));
  //             },
  //           });
  //         }
  //       }

  //       if (!linksArray.length) {
  //         linksArray = [
  //           {
  //             id: "no-external-links-option",
  //             key: "no-external-links",
  //             label: !links.length
  //               ? t("Files:NoExternalLinks")
  //               : t("Files:AllLinksAreDisabled"),
  //             disableColor: true,
  //           },
  //           !isMobile && {
  //             key: "separator0",
  //             isSeparator: true,
  //           },
  //           {
  //             icon: SettingsReactSvgUrl,
  //             id: "manage-option",
  //             key: "manage-links",
  //             label: t("Notifications:ManageNotifications"),
  //             onClick: () => this.onShowInfoPanel(item, "info_members"),
  //           },
  //         ];
  //       }

  //       this.setLoaderTimer(false, () => resolve(linksArray));
  //     } catch (error) {
  //       toastr.error(error);
  //       this.setLoaderTimer(false);
  //       return reject(linksArray);
  //     }
  //   });

  //   return promise;
  // };

  onLoadPlugins = (item) => {
    const { contextOptions } = item;
    const { enablePlugins } = this.settingsStore;

    const pluginItems = [];
    this.setLoaderTimer(true);

    if (enablePlugins && this.pluginStore.contextMenuItemsList) {
      this.pluginStore.contextMenuItemsList.forEach((option) => {
        if (contextOptions.includes(option.key)) {
          const value = option.value;

          const onClick = async () => {
            if (value.withActiveItem) {
              const { setActiveFiles } = this.filesStore;

              setActiveFiles([item.id]);

              await value.onClick(item.id);

              setActiveFiles([]);
            } else {
              value.onClick(item.id);
            }
          };

          if (value.fileExt) {
            if (value.fileExt.includes(item.fileExst)) {
              pluginItems.push({
                key: option.key,
                label: value.label,
                icon: value.icon,
                onClick,
              });
            }
          } else {
            pluginItems.push({
              key: option.key,
              label: value.label,
              icon: value.icon,
              onClick,
            });
          }
        }
      });
    }

    this.setLoaderTimer(false);

    return pluginItems;
  };

  onClickInviteUsers = (e, roomType) => {
    const data = (e.currentTarget && e.currentTarget.dataset) || e;

    const { action } = data;

    const { isGracePeriod } = this.currentTariffStatusStore;

    if (isGracePeriod) {
      this.dialogsStore.setInviteUsersWarningDialogVisible(true);
    } else {
      this.dialogsStore.setInvitePanelOptions({
        visible: true,
        roomId: action ? action : e,
        hideSelector: false,
        defaultAccess: getDefaultAccessUser(roomType),
      });
    }
  };

  onClickPin = (e, id, t) => {
    const data = (e.currentTarget && e.currentTarget.dataset) || e;
    const { action } = data;

    this.filesActionsStore.setPinAction(action, id, t);
  };

  onClickArchive = (e) => {
    const data = (e.currentTarget && e.currentTarget.dataset) || e;
    const { action } = data;
    const { isGracePeriod } = this.currentTariffStatusStore;
    const {
      setArchiveDialogVisible,
      setRestoreRoomDialogVisible,
      setInviteUsersWarningDialogVisible,
    } = this.dialogsStore;

    if (action === "unarchive" && isGracePeriod) {
      setInviteUsersWarningDialogVisible(true);
      return;
    }

    if (action === "archive") {
      setArchiveDialogVisible(true);
    } else {
      setRestoreRoomDialogVisible(true);
    }
  };

  onLeaveRoom = () => {
    this.dialogsStore.setLeaveRoomDialogVisible(true);
  };

  onSelect = (item) => {
    const { onSelectItem } = this.filesActionsStore;

    onSelectItem({ id: item.id, isFolder: item.isFolder }, true, false);
  };

  onShowEditingToast = (t) => {
    toastr.error(t("Files:DocumentEdited"));
  };

  onShowWaitOperationToast = (t) => {
    toastr.warning(t("Files:WaitOperation"));
  };

  onClickMute = (e, item, t) => {
    const data = (e.currentTarget && e.currentTarget.dataset) || e;
    const { action } = data;

    this.filesActionsStore.setMuteAction(action, item, t);
  };

  onClickRemoveFromRecent = (item) => {
    this.filesActionsStore.removeFilesFromRecent([item.id]);
  };

  setLoaderTimer = (isLoading, cb) => {
    if (isLoading) {
      loadingTime = new Date();

      return (timer = setTimeout(() => {
        this.linksIsLoading = true;
      }, LOADER_TIMER));
    } else {
      if (loadingTime) {
        const currentDate = new Date();

        let ms = Math.abs(loadingTime.getTime() - currentDate.getTime());

        if (timer) {
          let ms = Math.abs(ms - LOADER_TIMER);

          clearTimeout(timer);
          timer = null;
        }

        if (ms < LOADER_TIMER) {
          return setTimeout(() => {
            this.linksIsLoading = true;
            loadingTime = null;
            cb && cb();
          }, LOADER_TIMER - ms);
        }
      }

      if (timer) {
        clearTimeout(timer);
        timer = null;
      }

      loadingTime = null;
      this.linksIsLoading = false;
      cb && cb();
    }
  };

  onCreateOform = (navigate) => {
    this.infoPanelStore.setIsVisible(false);
    const filesFilter = FilesFilter.getDefault();
    filesFilter.folder = this.oformsStore.oformFromFolderId;
    const filterUrlParams = filesFilter.toUrlParams();
    const url = getCategoryUrl(
      this.filesStore.categoryType,
      filesFilter.folder,
    );

    navigate(
      combineUrl(
        window.ClientConfig?.proxy?.url,
        config.homepage,
        `${url}?${filterUrlParams}`,
      ),
    );
  };

  onShowOformTemplateInfo = (item) => {
    this.infoPanelStore.setIsVisible(true);
    this.oformsStore.setGallerySelected(item);
  };

  onSuggestOformChanges = (item) => {
    const formTitle = item.attributes ? item.attributes.name_form : item.title;

    window.location = `mailto:marketing@onlyoffice.com
    ?subject=Suggesting changes for ${formTitle}
    &body=Suggesting changes for ${formTitle}.
  `;
  };

  getFormGalleryContextOptions = (item, t, navigate) => {
    return [
      {
        key: "create",
        label: t("Common:Create"),
        onClick: () => this.onCreateOform(navigate),
      },
      {
        key: "template-info",
        label: t("FormGallery:TemplateInfo"),
        onClick: () => this.onShowOformTemplateInfo(item),
      },
      {
        key: "separator",
        isSeparator: true,
      },
      {
        key: "suggest-changes",
        label: t("FormGallery:SuggestChanges"),
        onClick: () => this.onSuggestOformChanges(item),
      },
    ];
  };

  getRoomsRootContextOptions = (item, t) => {
    const { id, rootFolderId } = this.selectedFolderStore;
    const isRootRoom = item.isRoom && rootFolderId === id;

    if (!isRootRoom) return { pinOptions: [], muteOptions: [] };

    const pinOptions = [
      {
        id: "option_pin-room",
        key: "pin-room",
        label: t("PinToTop"),
        icon: PinReactSvgUrl,
        onClick: (e) => this.onClickPin(e, item.id, t),
        disabled: this.publicRoomStore.isPublicRoom,
        "data-action": "pin",
        action: "pin",
      },
      {
        id: "option_unpin-room",
        key: "unpin-room",
        label: t("Unpin"),
        icon: UnpinReactSvgUrl,
        onClick: (e) => this.onClickPin(e, item.id, t),
        disabled: this.publicRoomStore.isPublicRoom,
        "data-action": "unpin",
        action: "unpin",
      },
    ];

    const muteOptions = [
      {
        id: "option_unmute-room",
        key: "unmute-room",
        label: t("EnableNotifications"),
        icon: UnmuteReactSvgUrl,
        onClick: (e) => this.onClickMute(e, item, t),
        disabled: !item.inRoom || this.publicRoomStore.isPublicRoom,
        "data-action": "unmute",
        action: "unmute",
      },
      {
        id: "option_mute-room",
        key: "mute-room",
        label: t("DisableNotifications"),
        icon: MuteReactSvgUrl,
        onClick: (e) => this.onClickMute(e, item, t),
        disabled: !item.inRoom || this.publicRoomStore.isPublicRoom,
        "data-action": "mute",
        action: "mute",
      },
    ];

    return { pinOptions, muteOptions };
  };

  onEmptyTrashAction = () => {
    const { activeFiles, activeFolders } = this.filesStore;
    const isExistActiveItems = [...activeFiles, ...activeFolders].length > 0;

    if (isExistActiveItems || this.filesActionsStore.emptyTrashInProgress)
      return;

    this.dialogsStore.setEmptyTrashDialogVisible(true);
  };

  onRestoreAllAction = () => {
    const { activeFiles, activeFolders } = this.filesStore;
    const isExistActiveItems = [...activeFiles, ...activeFolders].length > 0;

    if (isExistActiveItems) return;

    this.dialogsStore.setRestoreAllPanelVisible(true);
  };

  onRestoreAllArchiveAction = () => {
    const { activeFiles, activeFolders } = this.filesStore;
    const {
      setInviteUsersWarningDialogVisible,
      setRestoreAllArchive,
      setRestoreRoomDialogVisible,
    } = this.dialogsStore;

    const isExistActiveItems = [...activeFiles, ...activeFolders].length > 0;

    if (isExistActiveItems) return;

    if (this.currentTariffStatusStore.isGracePeriod) {
      setInviteUsersWarningDialogVisible(true);
      return;
    }

    setRestoreAllArchive(true);
    setRestoreRoomDialogVisible(true);
  };

  getHeaderOptions = (t, item) => {
    const { isRecycleBinFolder, isArchiveFolder } = this.treeFoldersStore;
    const { roomsForDelete, roomsForRestore } = this.filesStore;

    const canRestoreAll = roomsForRestore.length > 0;
    const canDeleteAll = roomsForDelete.length > 0;

    if (this.publicRoomStore.isPublicRoom) {
      return [
        {
          key: "public-room_share",
          label: t("Files:CopySharedLink"),
          icon: TabletLinkReactSvgUrl,
          onClick: () => {
            copy(window.location.href);
            toastr.success(t("Common:LinkCopySuccess"));
          },
          disabled: this.settingsStore.isFrame,
        },
        {
          key: "separator0",
          isSeparator: true,
          disabled: !item.security?.Download,
        },
        {
          key: "public-room_edit",
          label: t("Common:Download"),
          icon: DownloadReactSvgUrl,
          onClick: () => {
            this.onClickDownload(item, t);
          },
          disabled: !item.security?.Download,
        },
      ];
    }

    if (isRecycleBinFolder) {
      return [
        {
          id: "header_option_empty-trash",
          key: "empty-trash",
          label: t("Files:EmptyRecycleBin"),
          onClick: this.onEmptyTrashAction,
          icon: ClearTrashReactSvgUrl,
          disabled: false,
        },
        {
          id: "header_option_restore-all",
          key: "restore-all",
          label: t("RestoreAll"),
          onClick: this.onRestoreAllAction,
          icon: MoveReactSvgUrl,
          disabled: false,
        },
      ];
    }

    if (isArchiveFolder) {
      return [
        {
          id: "header_option_empty-archive",
          key: "empty-archive",
          label: t("ArchiveAction"),
          onClick: this.onEmptyTrashAction,
          disabled: !canDeleteAll,
          icon: ClearTrashReactSvgUrl,
        },
        {
          id: "header_option_restore-all",
          key: "restore-all",
          label: t("RestoreAll"),
          onClick: this.onRestoreAllArchiveAction,
          disabled: !canRestoreAll,
          icon: MoveReactSvgUrl,
        },
      ];
    }

    return this.getFilesContextOptions(item, t, false, true);
  };

  getFilesContextOptions = (item, t, isInfoPanel, isHeader) => {
    const optionsToRemove = isInfoPanel
      ? ["select", "room-info", "show-info"]
      : isHeader
        ? ["select"]
        : [];

    if (!item.contextOptions) {
      const contextOptions = this.filesStore.getFilesContextOptions(
        item,
        optionsToRemove,
      );
      item = { ...item, contextOptions };
    } else {
      item.contextOptions = this.filesStore.removeOptions(
        item.contextOptions,
        optionsToRemove,
      );
    }

    const { contextOptions, isEditing } = item;

    const isRootThirdPartyFolder =
      item.providerKey && item.id === item.rootFolderId;

    const isShareable = this.treeFoldersStore.isPersonalRoom
      ? item.canShare || (item.isFolder && item.security?.CreateRoomFrom)
      : false;

    const isMedia =
      item.viewAccessibility?.ImageView || item.viewAccessibility?.MediaView;

    const hasInfoPanel = contextOptions.includes("show-info");

    //const emailSendIsDisabled = true;
    const showSeparator0 = hasInfoPanel || !isMedia; // || !emailSendIsDisabled;

    const { isGroupMenuBlocked } = this.filesActionsStore;

    const separator0 = showSeparator0
      ? {
          key: "separator0",
          isSeparator: true,
        }
      : false;

    const onlyShowVersionHistory =
      !contextOptions.includes("finalize-version") &&
      contextOptions.includes("show-version-history");

    const versionActions = isDesktop()
      ? onlyShowVersionHistory
        ? [
            {
              id: "option_show-version-history",
              key: "show-version-history",
              label: t("ShowVersionHistory"),
              icon: HistoryReactSvgUrl,
              onClick: () =>
                this.showVersionHistory(
                  item.id,
                  item.security,
                  item?.requestToken,
                ),
              disabled: false,
            },
          ]
        : [
            {
              id: "option_version",
              key: "version",
              label: t("VersionHistory"),
              icon: HistoryFinalizedReactSvgUrl,
              items: [
                {
                  id: "option_finalize-version",
                  key: "finalize-version",
                  label: t("FinalizeVersion"),
                  icon: HistoryFinalizedReactSvgUrl,
                  onClick: () =>
                    isEditing
                      ? this.onShowEditingToast(t)
                      : this.finalizeVersion(item.id, item.security),
                  disabled: false,
                },
                {
                  id: "option_version-history",
                  key: "show-version-history",
                  label: t("ShowVersionHistory"),
                  icon: HistoryReactSvgUrl,
                  onClick: () =>
                    this.showVersionHistory(
                      item.id,
                      item.security,
                      item?.requestToken,
                    ),
                  disabled: false,
                },
              ],
            },
          ]
      : [
          {
            id: "option_finalize-version",
            key: "finalize-version",
            label: t("FinalizeVersion"),
            icon: HistoryFinalizedReactSvgUrl,
            onClick: () =>
              isEditing
                ? this.onShowEditingToast(t)
                : this.finalizeVersion(item.id),
            disabled: false,
          },
          {
            id: "option_version-history",
            key: "show-version-history",
            label: t("ShowVersionHistory"),
            icon: HistoryReactSvgUrl,
            onClick: () =>
              this.showVersionHistory(
                item.id,
                item.security,
                item?.requestToken,
              ),
            disabled: false,
          },
        ];
    const moveActions = isDesktop()
      ? [
          {
            id: "option_move-or-copy",
            key: "move",
            label: t("MoveOrCopy"),
            icon: CopyReactSvgUrl,
            items: [
              {
                id: "option_move-to",
                key: "move-to",
                label: t("Common:MoveTo"),
                icon: MoveReactSvgUrl,
                onClick: isEditing
                  ? () => this.onShowEditingToast(t)
                  : () => this.onMoveAction(item),
                disabled: false,
              },
              {
                id: "option_copy-to",
                key: "copy-to",
                label: t("Common:Copy"),
                icon: CopyReactSvgUrl,
                onClick: () => this.onCopyAction(item),
                disabled: false,
              },
              {
                id: "option_create-duplicate",
                key: "duplicate",
                label: t("Common:Duplicate"),
                icon: DuplicateReactSvgUrl,
                onClick: () => this.onDuplicate(item, t),
                disabled: false,
              },
            ],
          },
        ]
      : [
          {
            id: "option_move-to",
            key: "move-to",
            label: t("Common:MoveTo"),
            icon: MoveReactSvgUrl,
            onClick: isEditing
              ? () => this.onShowEditingToast(t)
              : () => this.onMoveAction(item),
            disabled: false,
          },
          {
            id: "option_copy-to",
            key: "copy-to",
            label: t("Common:Copy"),
            icon: CopyReactSvgUrl,
            onClick: () => this.onCopyAction(item),
            disabled: false,
          },
          {
            id: "option_create-duplicate",
            key: "duplicate",
            label: t("Common:Duplicate"),
            icon: DuplicateReactSvgUrl,
            onClick: () => this.onDuplicate(item, t),
            disabled: false,
          },
        ];

    const { pinOptions, muteOptions } = this.getRoomsRootContextOptions(
      item,
      t,
    );

    let withOpen = item.id !== this.selectedFolderStore.id;
    const isPublicRoomType =
      item.roomType === RoomsType.PublicRoom ||
      item.roomType === RoomsType.FormRoom ||
      item.roomType === RoomsType.CustomRoom;

    const { navigationPath } = this.selectedFolderStore;

    if (item.isRoom && withOpen) {
      withOpen = navigationPath.findIndex((f) => f.id === item.id) === -1;
    }

    const isArchive = item.rootFolderType === FolderType.Archive;

    const hasShareLinkRights = item.shared
      ? item.security?.Read
      : item.security?.EditAccess;

    const optionsModel = [
      {
        id: "option_select",
        key: "select",
        label: t("Common:SelectAction"),
        icon: CheckBoxReactSvgUrl,
        onClick: () => this.onSelect(item),
        disabled: false,
      },
      withOpen && {
        id: "option_open",
        key: "open",
        label: t("Open"),
        icon: FolderReactSvgUrl,
        onClick: () => this.onOpenFolder(item),
        disabled: false,
      },
      {
        id: "option_fill-form",
        key: "fill-form",
        label: t("Common:FillFormButton"),
        icon: FormFillRectSvgUrl,
        onClick: () => this.onClickLinkFillForm(item),
        disabled: false,
      },
      {
        id: "option_open-pdf",
        key: "open-pdf",
        label: t("Open"),
        icon: EyeReactSvgUrl,
        onClick: () => this.gotoDocEditor(false, item),
        disabled: false,
      },
      {
        id: "option_edit-pdf",
        key: "edit-pdf",
        label: t("Common:EditButton"),
        icon: AccessEditReactSvgUrl,
        onClick: () => this.onOpenPDFEditDialog(item.id),
        disabled: false,
      },
      {
        id: "option_edit",
        key: "edit",
        label: t("Common:EditButton"),
        icon: AccessEditReactSvgUrl,
        onClick: () => this.onClickLinkEdit(item),
        disabled: false,
      },
      {
        id: "option_preview",
        key: "preview",
        label: t("Common:Preview"),
        icon: EyeReactSvgUrl,
        onClick: () => this.onPreviewClick(item),
        disabled: false,
      },
      {
        id: "option_view",
        key: "view",
        label: t("Common:View"),
        icon: EyeReactSvgUrl,
        onClick: (fileId) => this.onMediaFileClick(fileId, item),
        disabled: false,
      },
      {
        id: "option_pdf-view",
        key: "pdf-view",
        label: "Pdf viewer",
        icon: EyeReactSvgUrl,
        onClick: (fileId) => this.onMediaFileClick(fileId, item),
        disabled: false,
      },
      {
        id: "option_make-form",
        key: "make-form",
        label: t("Common:MakeForm"),
        icon: FormPlusReactSvgUrl,
        onClick: () => this.onClickMakeForm(item, t),
        disabled: false,
      },
      separator0,
      {
        id: "option_submit-to-gallery",
        key: "submit-to-gallery",
        label: t("Common:SubmitToFormGallery"),
        icon: FormFileReactSvgUrl,
        onClick: () => this.onClickSubmitToFormGallery(item),
        isOutsideLink: true,
        disabled: !item.security?.SubmitToFormGallery,
      },
      {
        key: "separator-SubmitToGallery",
        isSeparator: true,
      },
      {
        id: "option_reconnect-storage",
        key: "reconnect-storage",
        label: t("Common:ReconnectStorage"),
        icon: ReconnectSvgUrl,
        onClick: () => this.onClickReconnectStorage(item, t),
        disabled: !item.security?.Reconnect || !item.security?.EditRoom,
      },
      {
        id: "option_edit-room",
        key: "edit-room",
        label: t("EditRoom"),
        icon: SettingsReactSvgUrl,
        onClick: () => this.onClickEditRoom(item),
        disabled: false,
      },
      {
        id: "option_invite-users-to-room",
        key: "invite-users-to-room",
        label: t("Common:InviteUsers"),
        icon: PersonReactSvgUrl,
        onClick: (e) => this.onClickInviteUsers(e, item.roomType),
        disabled: false,
        action: item.id,
      },
      {
        id: "option_copy-general-link",
        key: "copy-general-link",
        label: t("Files:CopySharedLink"),
        icon: TabletLinkReactSvgUrl,
        disabled: !isShareable,
        onClick: async () => {
          const { getPrimaryFileLink, setShareChanged } = this.infoPanelStore;

          const primaryLink = await getPrimaryFileLink(item.id);
          if (primaryLink) {
            copyShareLink(primaryLink.sharedTo.shareLink);
            item.shared
              ? toastr.success(t("Files:LinkSuccessfullyCopied"))
              : toastr.success(t("Files:LinkSuccessfullyCreatedAndCopied"));
            setShareChanged(true);
          }
        },
      },
      {
        id: "option_sharing-settings",
        key: "sharing-settings",
        label: t("Common:Share"),
        icon: ShareReactSvgUrl,
        onClick: () => this.onClickShare(item),
        disabled: !isShareable,
      },
      ...versionActions,
      {
        id: "option_link-for-room-members",
        key: "link-for-room-members",
        label: t("Files:CopyLink"),
        icon: InvitationLinkReactSvgUrl,
        onClick: () => this.onCopyLink(item, t),
        disabled: isPublicRoomType && hasShareLinkRights,
      },
      {
        id: "option_copy-external-link",
        key: "external-link",
        label: t("Files:CopySharedLink"),
        icon: TabletLinkReactSvgUrl,
        disabled: !hasShareLinkRights,
        onClick: () => this.onCreateAndCopySharedLink(item, t),
        // onLoad: () => this.onLoadLinks(t, item),
      },
      {
        id: "option_room-info",
        key: "room-info",
        label: t("Common:Info"),
        icon: InfoOutlineReactSvgUrl,
        onClick: () => this.onShowInfoPanel(item),
        disabled: this.publicRoomStore.isPublicRoom,
      },
      ...pinOptions,
      ...muteOptions,
      {
        id: "option_owner-change",
        key: "owner-change",
        label: t("Translations:OwnerChange"),
        icon: FileActionsOwnerReactSvgUrl,
        onClick: this.onOwnerChange,
        disabled: false,
      },
      {
        id: "option_link-for-portal-users",
        key: "link-for-portal-users",
        label: t("LinkForPortalUsers", {
          productName: t("Common:ProductName"),
        }),
        icon: InvitationLinkReactSvgUrl,
        onClick: () => this.onClickLinkForPortal(item, t),
        disabled: false,
      },
      // {
      //   id: "option_send-by-email",
      //   key: "send-by-email",
      //   label: t("SendByEmail"),
      //   icon: MailReactSvgUrl,
      //   disabled: emailSendIsDisabled,
      // },
      {
        id: "option_embedding-setting",
        key: "embedding-settings",
        label: t("Files:Embed"),
        icon: CodeReactSvgUrl,
        onClick: () => this.onOpenEmbeddingSettings(item),
        disabled: !item.security?.Embed,
      },
      {
        id: "option_show-info",
        key: "show-info",
        label: t("Common:Info"),
        icon: InfoOutlineReactSvgUrl,
        onClick: () => this.onShowInfoPanel(item),
        disabled: false,
      },
      {
        id: "option_block-unblock-version",
        key: "block-unblock-version",
        label: t("UnblockVersion"),
        icon: LockedReactSvgUrl,
        onClick: () => this.lockFile(item, t),
        disabled: false,
      },
      {
        key: "separator1",
        isSeparator: true,
      },
      {
        id: "option_open-location",
        key: "open-location",
        label: t("OpenLocation"),
        icon: FolderLocationReactSvgUrl,
        onClick: () => this.onOpenLocation(item),
        disabled: false,
      },
      {
        id: "option_mark-read",
        key: "mark-read",
        label: t("MarkRead"),
        icon: TickRoundedSvgUrl,
        onClick: () => this.onClickMarkRead(item),
        disabled: false,
      },
      {
        id: "option_mark-as-favorite",
        key: "mark-as-favorite",
        label: t("MarkAsFavorite"),
        icon: FavoritesReactSvgUrl,
        onClick: (e) => this.onClickFavorite(e, item.id, t),
        disabled: false,
        "data-action": "mark",
        action: "mark",
      },
      {
        id: "option_remove-from-favorites",
        key: "remove-from-favorites",
        label: t("RemoveFromFavorites"),
        icon: FavoritesReactSvgUrl,
        onClick: (e) => this.onClickFavorite(e, item.id, t),
        disabled: false,
        "data-action": "remove",
        action: "remove",
      },
      {
        id: "option_create_room",
        key: "create-room",
        label: t("Files:CreateRoom"),
        icon: CatalogRoomsReactSvgUrl,
        onClick: () => this.onClickCreateRoom(item),
        disabled: !item.security?.CreateRoomFrom,
      },
      {
        id: "option_create-duplicate-room",
        key: "duplicate-room",
        label: t("Common:Duplicate"),
        icon: DuplicateReactSvgUrl,
        onClick: () => this.onDuplicate(item, t),
        disabled: !item.security?.Duplicate,
      },
      {
        id: "option_download",
        key: "download",
        label: t("Common:Download"),
        icon: DownloadReactSvgUrl,
        onClick: () => this.onClickDownload(item, t),
        disabled: !item.security?.Download,
      },
      {
        id: "option_download-as",
        key: "download-as",
        label: t("Translations:DownloadAs"),
        icon: DownloadAsReactSvgUrl,
        onClick: this.onClickDownloadAs,
        disabled: !item.security?.Download,
      },
      ...moveActions,
      {
        id: "option_restore",
        key: "restore",
        label: t("Common:Restore"),
        icon: MoveReactSvgUrl,
        onClick: this.onRestoreAction,
        disabled: false,
      },
      {
        id: "option_rename",
        key: "rename",
        label: t("Common:Rename"),
        icon: RenameReactSvgUrl,
        onClick: () => this.onClickRename(item),
        disabled: false,
      },
      {
        key: "separator3",
        isSeparator: true,
      },
      {
        id: "option_unsubscribe",
        key: "unsubscribe",
        label: t("RemoveFromList"),
        icon: RemoveSvgUrl,
        onClick: this.onClickUnsubscribe,
        disabled: false,
      },
      {
        id: "option_change-thirdparty-info",
        key: "change-thirdparty-info",
        label: t("Translations:ThirdPartyInfo"),
        icon: AccessEditReactSvgUrl,
        onClick: () => this.onChangeThirdPartyInfo(item.providerKey),
        disabled: false,
      },
      {
        id: "option_archive-room",
        key: "archive-room",
        label: t("MoveToArchive"),
        icon: RoomArchiveSvgUrl,
        onClick: (e) => this.onClickArchive(e),
        disabled: false,
        "data-action": "archive",
        action: "archive",
      },
      {
        id: "option_leave-room",
        key: "leave-room",
        label: t("LeaveTheRoom"),
        icon: LeaveRoomSvgUrl,
        onClick: this.onLeaveRoom,
        disabled:
          isArchive || !item.inRoom || this.publicRoomStore.isPublicRoom,
      },
      {
        id: "option_unarchive-room",
        key: "unarchive-room",
        label: t("Common:Restore"),
        icon: MoveReactSvgUrl,
        onClick: (e) => this.onClickArchive(e),
        disabled: false,
        "data-action": "unarchive",
        action: "unarchive",
      },
      {
        id: "option_delete",
        key: "delete",
        label: isRootThirdPartyFolder
          ? t("Common:Disconnect")
          : t("Common:Delete"),
        icon: TrashReactSvgUrl,
        onClick: () =>
          isEditing
            ? this.onShowEditingToast(t)
            : isGroupMenuBlocked
              ? this.onShowWaitOperationToast(t)
              : this.onClickDelete(item, t),
        disabled: false,
      },
      {
        id: "option_remove-from-recent",
        key: "remove-from-recent",
        label: t("RemoveFromList"),
        icon: RemoveOutlineSvgUrl,
        onClick: () => this.onClickRemoveFromRecent(item),
        disabled: !this.treeFoldersStore.isRecentTab,
      },
    ];

    const options = this.filterModel(optionsModel, contextOptions);

    const pluginItems = this.onLoadPlugins(item);

    if (pluginItems.length > 0) {
      if (!isDesktop() || pluginItems.length === 1) {
        pluginItems.forEach((plugin) => {
          options.splice(1, 0, {
            id: `option_${plugin.key}`,
            key: plugin.key,
            label: plugin.label,
            icon: plugin.icon,
            disabled: false,
            onClick: plugin.onClick,
          });
        });
      } else {
        options.splice(1, 0, {
          id: "option_plugin-actions",
          key: "plugin_actions",
          label: t("Common:Actions"),
          icon: PluginActionsSvgUrl,
          disabled: false,

          onLoad: () => this.onLoadPlugins(item),
        });
      }
    }

    const { isCollaborator } = this.userStore?.user || {
      isCollaborator: false,
    };

    const newOptions = options.filter(
      (option, index) =>
        !(index === 0 && option.key === "separator1") &&
        !(isCollaborator && option.key === "create-room"),
    );

    return trimSeparator(newOptions);
  };

  getGroupContextOptions = (t) => {
    const { selection, allFilesIsEditing } = this.filesStore;
    const { setDeleteDialogVisible } = this.dialogsStore;
    const { isRecycleBinFolder, isRoomsFolder, isArchiveFolder } =
      this.treeFoldersStore;

    const { pinRooms, unpinRooms, deleteRooms } = this.filesActionsStore;

    if (isRoomsFolder || isArchiveFolder) {
      const isPinOption = selection.filter((item) => !item.pinned).length > 0;

      let canDelete;
      if (isRoomsFolder) {
        canDelete = selection.every((k) => k.contextOptions.includes("delete"));
      } else if (isArchiveFolder) {
        canDelete = selection.some((k) => k.contextOptions.includes("delete"));
      }

      const canArchiveRoom = selection.every((k) =>
        k.contextOptions.includes("archive-room"),
      );

      const canRestoreRoom = selection.some((k) =>
        k.contextOptions.includes("unarchive-room"),
      );

      let archiveOptions;

      const pinOption = isPinOption
        ? {
            key: "pin-room",
            label: t("PinToTop"),
            icon: PinReactSvgUrl,
            onClick: () => pinRooms(t),
            disabled: false,
          }
        : {
            key: "unpin-room",
            label: t("Unpin"),
            icon: UnpinReactSvgUrl,
            onClick: () => unpinRooms(t),
            disabled: false,
          };

      if (canArchiveRoom) {
        archiveOptions = {
          key: "archive-room",
          label: t("MoveToArchive"),
          icon: RoomArchiveSvgUrl,
          onClick: (e) => this.onClickArchive(e),
          disabled: false,
          "data-action": "archive",
          action: "archive",
        };
      }
      if (canRestoreRoom) {
        archiveOptions = {
          key: "unarchive-room",
          label: t("Common:Restore"),
          icon: MoveReactSvgUrl,
          onClick: (e) => this.onClickArchive(e),
          disabled: false,
          "data-action": "unarchive",
          action: "unarchive",
        };
      }

      const options = [];

      if (!isArchiveFolder) {
        options.push(pinOption);
      }

      if ((canArchiveRoom || canDelete) && !isArchiveFolder) {
        options.push({
          key: "separator0",
          isSeparator: true,
        });
      }

      options.push(archiveOptions);

      canDelete &&
        options.push({
          key: "delete-rooms",
          label: t("Common:Delete"),
          icon: TrashReactSvgUrl,
          onClick: () => deleteRooms(t),
        });

      return options;
    }

    const hasDownloadAccess =
      selection.findIndex((k) => k.security.Download) !== -1;

    const favoriteItems = selection.filter((k) =>
      k.contextOptions?.includes("mark-as-favorite"),
    );

    const moveItems = selection.filter((k) =>
      k.contextOptions.includes("move-to"),
    ).length;

    const copyItems = selection.filter((k) =>
      k.contextOptions.includes("copy-to"),
    ).length;

    const restoreItems = selection.filter((k) =>
      k.contextOptions.includes("restore"),
    ).length;

    const removeFromFavoriteItems = selection.filter((k) =>
      k.contextOptions.includes("remove-from-favorites"),
    );

    const deleteItems = selection.filter((k) =>
      k.contextOptions.includes("delete"),
    ).length;

    const isRootThirdPartyFolder = selection.some(
      (x) => x.providerKey && x.id === x.rootFolderId,
    );

    const favoriteItemsIds = favoriteItems.map((item) => item.id);

    const removeFromFavoriteItemsIds = removeFromFavoriteItems.map(
      (item) => item.id,
    );

    const options = [
      {
        key: "mark-as-favorite",
        label: t("MarkAsFavorite"),
        icon: FavoritesReactSvgUrl,
        onClick: (e) => this.onClickFavorite(e, favoriteItemsIds, t),
        disabled: !favoriteItems.length,
        "data-action": "mark",
        action: "mark",
      },
      {
        key: "remove-from-favorites",
        label: t("RemoveFromFavorites"),
        icon: FavoritesReactSvgUrl,
        onClick: (e) => this.onClickFavorite(e, removeFromFavoriteItemsIds, t),
        disabled: favoriteItems.length || !removeFromFavoriteItems.length,
        "data-action": "remove",
        action: "remove",
      },
      {
        id: "create_room",
        key: "create-room",
        label: t("Files:CreateRoom"),
        icon: CatalogRoomsReactSvgUrl,
        onClick: this.onClickCreateRoom,
        disabled: !selection.security?.CreateRoomFrom,
      },
      {
        key: "download",
        label: t("Common:Download"),
        icon: DownloadReactSvgUrl,
        onClick: () =>
          this.filesActionsStore
            .downloadAction(t("Translations:ArchivingData"))
            .catch((err) => toastr.error(err)),
        disabled: !hasDownloadAccess,
      },
      {
        key: "download-as",
        label: t("Translations:DownloadAs"),
        icon: DownloadAsReactSvgUrl,
        onClick: this.onClickDownloadAs,
        disabled: !hasDownloadAccess,
      },
      {
        key: "move-to",
        label: t("Common:MoveTo"),
        icon: MoveReactSvgUrl,
        onClick: allFilesIsEditing
          ? () => this.onShowEditingToast(t)
          : this.onMoveAction,
        disabled: isRecycleBinFolder || !moveItems,
      },
      {
        key: "copy-to",
        label: t("Common:Copy"),
        icon: CopyReactSvgUrl,
        onClick: this.onCopyAction,
        disabled: isRecycleBinFolder || !copyItems,
      },
      {
        key: "restore",
        label: t("Common:Restore"),
        icon: MoveReactSvgUrl,
        onClick: this.onRestoreAction,
        disabled: !isRecycleBinFolder || !restoreItems,
      },
      {
        key: "separator1",
        isSeparator: true,
        disabled: !deleteItems || isRootThirdPartyFolder,
      },
      {
        key: "remove-from-recent",
        label: t("RemoveFromList"),
        icon: RemoveOutlineSvgUrl,
        onClick: () =>
          this.filesActionsStore.onClickRemoveFromRecent(selection),
        disabled: !this.treeFoldersStore.isRecentTab,
      },
      {
        key: "delete",
        label: t("Common:Delete"),
        icon: TrashReactSvgUrl,
        onClick: allFilesIsEditing
          ? () => this.onShowEditingToast(t)
          : () => {
              if (this.filesSettingsStore.confirmDelete) {
                setDeleteDialogVisible(true);
              } else {
                const translations = {
                  deleteOperation: t("Translations:DeleteOperation"),
                  deleteFromTrash: t("Translations:DeleteFromTrash"),
                  deleteSelectedElem: t("Translations:DeleteSelectedElem"),
                  FileRemoved: t("Files:FileRemoved"),
                  FolderRemoved: t("Files:FolderRemoved"),
                };

                this.filesActionsStore
                  .deleteAction(translations)
                  .catch((err) => toastr.error(err));
              }
            },
        disabled: !deleteItems || isRootThirdPartyFolder,
      },
    ];

    const { isCollaborator } = this.userStore?.user || {
      isCollaborator: false,
    };

    const newOptions = options.filter(
      (option, index) =>
        !(index === 0 && option.key === "separator1") &&
        !(isCollaborator && option.key === "create-room"),
    );

    return newOptions;
  };

  onInvite = (e) => {
    const { setInviteUsersWarningDialogVisible, setInvitePanelOptions } =
      this.dialogsStore;

    const type = e.item["data-type"];

    if (this.currentTariffStatusStore.isGracePeriod) {
      setInviteUsersWarningDialogVisible(true);
      return;
    }

    setInvitePanelOptions({
      visible: true,
      roomId: -1,
      hideSelector: true,
      defaultAccess: type,
    });
  };

  onInviteAgain = (t) => {
    resendInvitesAgain()
      .then(() =>
        toastr.success(t("PeopleTranslations:SuccessSentMultipleInvitatios")),
      )
      .catch((err) => toastr.error(err));
  };

  onCreateGroup = () => {
    const event = new Event(Events.GROUP_CREATE);
    window.dispatchEvent(event);
  };

  onCreateRoom = () => {
    if (this.currentTariffStatusStore.isGracePeriod) {
      this.dialogsStore.setInviteUsersWarningDialogVisible(true);
      return;
    }

    const event = new Event(Events.ROOM_CREATE);
    window.dispatchEvent(event);
  };

  onCreate = (format) => {
    const event = new Event(Events.CREATE);

    const isPDf = format === FileExtensions.PDF;

    const payload = {
      extension: format,
      id: -1,
      edit: isPDf,
    };

    event.payload = payload;

    window.dispatchEvent(event);
  };

  onCreateFormFromFile = () => {
    this.dialogsStore.setSelectFileDialogVisible(true);
  };

  onShowGallery = () => {
    const { oformsFilter } = this.oformsStore;

    const initOformFilter = (
      oformsFilter || oformsFilter.getDefault()
    ).toUrlParams();

    window.DocSpace.navigate(
      `/form-gallery/${this.selectedFolderStore.id}/filter?${initOformFilter}`,
    );
  };

  // TODO: add privacy room check for files
  onUploadAction = (type) => {
    const element =
      type === "file"
        ? document.getElementById("customFileInput")
        : type === "pdf"
          ? document.getElementById("customPDFInput")
          : document.getElementById("customFolderInput");

    element?.click();
  };

  onShowFormRoomSelectFileDialog = (filter = FilesSelectorFilterTypes.DOCX) => {
    this.dialogsStore.setSelectFileFormRoomDialogVisible(true, filter);
  };

  getContextOptionsPlusFormRoom = (t, actions) => {
    // const {
    //   // createTemplateForm,
    //   // createTemplateSelectFormFile,
    //   // templateOformsGallery,
    //   // createNewFolder,
    //   // createNewDoc,
    //   // createNewPresentation,
    //   // createNewSpreadsheet,
    //   // uploadFiles,
    //   // uploadFolder,
    // } = actions;

    // const templatePDFForm = {
    //   id: "personal_template-PDF-form",
    //   className: "main-button_drop-down",
    //   icon: FormReactSvgUrl,
    //   label: t("Common:CreatePDFForm"),
    //   key: "new-form",
    //   items: [createTemplateForm, createTemplateSelectFormFile],
    // };

    const showSelectorFormRoomDocx = {
      id: "actions_form-room_template_from-file",
      className: "main-button_drop-down_sub",
      icon: FormGalleryReactSvgUrl,
      label: t("Common:ChooseFromTemplates"),
      onClick: () => this.onShowGallery(),
      key: "form-file",
    };

    const uploadReadyPDFFrom = {
      id: "personal_upload-ready-Pdf-from",
      className: "main-button_drop-down_sub",
      icon: ActionsUploadReactSvgUrl,
      label: t("Common:UploadPDFForm"),
      key: "personal_upload-ready-Pdf-from",
      items: [
        {
          id: "personal_upload-from-docspace",
          className: "main-button_drop-down",
          icon: ActionsUploadReactSvgUrl,
          label: t("Common:FromPortal", {
            productName: t("Common:ProductName"),
          }),
          key: "personal_upload-from-docspace",
          onClick: () =>
            this.onShowFormRoomSelectFileDialog(FilterType.PDFForm),
        },
        {
          id: "personal_upload-from-device",
          className: "main-button_drop-down",
          icon: ActionsUploadReactSvgUrl,
          label: t("Common:FromDevice"),
          key: "personal_upload-from-device",
          onClick: () => this.onUploadAction("pdf"),
        },
      ],
    };

    const createNewFolder = {
      id: "actions_new-folder",
      className: "main-button_drop-down",
      icon: CatalogFolderReactSvgUrl,
      label: t("Files:CreateNewFolder"),
      onClick: () => this.onCreate(),
      key: "new-folder",
    };

    // const showUploadFolder = !(isMobile || isTablet);

    // const moreActions = {
    //   id: "personal_more-form",
    //   className: "main-button_drop-down",
    //   icon: PluginMoreReactSvgUrl,
    //   label: t("Common:More"),
    //   disabled: false,
    //   key: "more-form",
    //   items: [
    //     createNewFolder,
    //     {
    //       isSeparator: true,
    //       key: "personal_more-form__separator-1",
    //     },
    //     createNewDoc,
    //     createNewPresentation,
    //     createNewSpreadsheet,
    //     {
    //       isSeparator: true,
    //       key: "personal_more-form__separator-2",
    //     },
    //     uploadFiles,
    //     showUploadFolder ? uploadFolder : null,
    //   ],
    // };

    return [
      uploadReadyPDFFrom,
      showSelectorFormRoomDocx,
      // templateOformsGallery,
      // templatePDFForm,
      // {
      //   isSeparator: true,
      //   key: "separator",
      // },
      {
        isSeparator: true,
        key: "separator-1",
      },
      createNewFolder,
      // moreActions,
    ];
  };

  getFolderModel = (t, isSectionMenu) => {
    const { isLoading } = this.clientLoadingStore;
    const { security, roomType, parentRoomType, isFolder } =
      this.selectedFolderStore;
    const { isPublicRoom } = this.publicRoomStore;

    const isAccountsPage =
      window?.DocSpace?.location.pathname.includes("/accounts");

    const stateCanCreate = window?.DocSpace?.location?.state?.canCreate;
    const isSettingsPage =
      window?.DocSpace?.location.pathname.includes("/settings");

    const currentCanCreate =
      isLoading &&
      window?.DocSpace?.location?.state?.hasOwnProperty("canCreate")
        ? stateCanCreate
        : security?.Create;

    const groupId = new URLSearchParams(window.location.search).get("group");

    const isInsideGroup = !!groupId;

    const canCreate =
      (currentCanCreate || isAccountsPage) &&
      !isSettingsPage &&
      !isPublicRoom &&
      !isInsideGroup;

    const someDialogIsOpen = checkDialogsOpen();

    if (!canCreate || (isSectionMenu && (isMobile || someDialogIsOpen)))
      return null;

    const isOwner = this.userStore.user?.isOwner;
    const isRoomAdmin = this.userStore.user?.isRoomAdmin;
    const { isRoomsFolder, isPrivacyFolder } = this.treeFoldersStore;
    const { mainButtonItemsList } = this.pluginStore;
    const { enablePlugins } = this.settingsStore;
    const isFormRoomType =
      roomType === RoomsType.FormRoom ||
      (parentRoomType === FolderType.FormRoom && isFolder);

    const accountsUserOptions = [
      isOwner && {
        id: "accounts-add_administrator",
        className: "main-button_drop-down",
        icon: PersonAdminReactSvgUrl,
        label: t("Common:PortalAdmin", {
          productName: t("Common:ProductName"),
        }),
        onClick: this.onInvite,
        "data-type": EmployeeType.Admin,
        key: "administrator",
      },
      {
        id: "accounts-add_manager",
        className: "main-button_drop-down",
        icon: PersonManagerReactSvgUrl,
        label: t("Common:RoomAdmin"),
        onClick: this.onInvite,
        "data-type": EmployeeType.User,
        key: "manager",
      },
      {
        id: "accounts-add_collaborator",
        className: "main-button_drop-down",
        icon: PersonDefaultReactSvgUrl,
        label: t("Common:PowerUser"),
        onClick: this.onInvite,
        "data-type": EmployeeType.Collaborator,
        key: "collaborator",
      },
      {
        id: "accounts-add_user",
        className: "main-button_drop-down",
        icon: PersonDefaultReactSvgUrl,
        label: t("Common:User"),
        onClick: this.onInvite,
        "data-type": EmployeeType.Guest,
        key: "user",
      },
      {
        key: "separator",
        isSeparator: true,
      },
      {
        id: "accounts-add_invite-again",
        className: "main-button_drop-down",
        icon: InviteAgainReactSvgUrl,
        label: t("People:LblInviteAgain"),
        onClick: () => this.onInviteAgain(t),
        "data-action": "invite-again",
        key: "invite-again",
      },
    ];

    const accountsFullOptions = [
      {
        id: "actions_invite_user",
        className: "main-button_drop-down",
        icon: PersonUserReactSvgUrl,
        label: t("Common:Invite"),
        key: "new-user",
        items: accountsUserOptions,
      },
      {
        id: "create_group",
        className: "main-button_drop-down",
        icon: GroupReactSvgUrl,
        label: t("PeopleTranslations:CreateGroup"),
        onClick: this.onCreateGroup,
        action: "group",
        key: "group",
      },
    ];

    if (isAccountsPage) {
      return isRoomAdmin ? accountsUserOptions : accountsFullOptions;
    }

    const createNewDoc = {
      id: "personal_new-documnet",
      key: "new-document",
      label: t("Common:NewDocument"),
      onClick: () => this.onCreate("docx"),
      icon: ActionsDocumentsReactSvgUrl,
    };

    const createNewSpreadsheet = {
      id: "personal_new-spreadsheet",
      key: "new-spreadsheet",
      label: t("Common:NewSpreadsheet"),
      onClick: () => this.onCreate("xlsx"),
      icon: SpreadsheetReactSvgUrl,
    };

    const createNewPresentation = {
      id: "personal_new-presentation",
      key: "new-presentation",
      label: t("Common:NewPresentation"),
      onClick: () => this.onCreate("pptx"),
      icon: ActionsPresentationReactSvgUrl,
    };

    const createTemplateForm = {
      id: "personal_template_black",
      key: "new-form",
      label: t("Translations:SubNewForm"),
      icon: FormBlankReactSvgUrl,
      onClick: () => this.onCreate("pdf"),
    };

    const createTemplateNewFormFile = {
      id: "personal_template_new-form-file",
      key: "new-form-file",
      label: t("Translations:SubNewFormFile"),
      icon: FormFileReactSvgUrl,
      onClick: this.onCreateFormFromFile,
      disabled: isPrivacyFolder,
    };

    const createTemplateSelectFormFile = {
      id: "personal_template_new-form-file",
      key: "new-form-file",
      label: t("Translations:SubNewFormFile"),
      icon: FormFileReactSvgUrl,
      onClick: this.onCreateFormFromFile,
      disabled: isPrivacyFolder,
    };

    const templateOformsGallery = {
      id: "personal_template_oforms-gallery",
      key: "oforms-gallery",
      label: t("Common:OFORMsGallery"),
      icon: FormGalleryReactSvgUrl,
      onClick: () => this.onShowGallery(),
      disabled: isPrivacyFolder,
    };

    const createNewFolder = {
      id: "personal_new-folder",
      key: "new-folder",
      label: t("Common:NewFolder"),
      onClick: () => this.onCreate(),
      icon: CatalogFolderReactSvgUrl,
    };

    const uploadFiles = {
      key: "upload-files",
      label: t("Article:UploadFiles"),
      onClick: () => this.onUploadAction("file"),
      icon: ActionsUploadReactSvgUrl,
    };

    const uploadFolder = {
      key: "upload-folder",
      label: t("Article:UploadFolder"),
      onClick: () => this.onUploadAction("folder"),
      icon: ActionsUploadReactSvgUrl,
    };

    if (isFormRoomType) {
      return this.getContextOptionsPlusFormRoom(t, {
        createTemplateForm,
        createTemplateSelectFormFile,
        templateOformsGallery,
        createNewFolder,
        createNewDoc,
        createNewPresentation,
        createNewSpreadsheet,
        uploadFiles,
        uploadFolder,
      });
    }

    const formActions = isDesktop()
      ? [
          {
            id: "personal_form-template",
            icon: FormReactSvgUrl,
            label: t("Translations:NewForm"),
            key: "new-form-base",
            items: [
              createTemplateForm,
              createTemplateNewFormFile,
              templateOformsGallery,
            ],
          },
        ]
      : [createTemplateForm, createTemplateNewFormFile, templateOformsGallery];

    const showUploadFolder = !(isMobile || isTablet);
    const options = isRoomsFolder
      ? [
          {
            key: "new-room",
            label: t("Common:NewRoom"),
            onClick: this.onCreateRoom,
            icon: CatalogRoomsReactSvgUrl,
          },
        ]
      : [
          createNewDoc,
          createNewSpreadsheet,
          createNewPresentation,
          ...formActions,
          createNewFolder,
          { key: "separator", isSeparator: true },
          uploadFiles,
          showUploadFolder ? uploadFolder : null,
        ];

    if (mainButtonItemsList && enablePlugins && !isRoomsFolder) {
      const pluginItems = [];

      mainButtonItemsList.forEach((option) => {
        pluginItems.push({
          key: option.key,
          ...option.value,
        });
      });

      options.splice(5, 0, {
        id: "actions_more-plugins",
        className: "main-button_drop-down",
        icon: PluginMoreReactSvgUrl,
        label: t("Common:More"),
        disabled: false,
        key: "more-plugins",
        items: pluginItems,
      });
    }

    return options;
  };

  getModel = (item, t) => {
    const { selection } = this.filesStore;

    const { fileExst, contextOptions } = item;

    const contextOptionsProps =
      contextOptions && contextOptions.length > 0
        ? selection.length > 1
          ? this.getGroupContextOptions(t)
          : this.getFilesContextOptions(item, t)
        : [];

    return contextOptionsProps;
  };
}

export default ContextOptionsStore;
